<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud"/>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission"/>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="故事名称">
            <el-input v-model="form.title" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="故事描述">
            <el-input :rows="3" v-model="form.content" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="产品分类">
            <el-select v-model="form.productType" filterable placeholder="请选择">
              <el-option
                  v-for="item in category"
                  :key="item.id"
                  :label="item.cateName"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="图片地址">
            <MaterialList
                v-model="picArr"
                :height="150"
                :num="1"
                :width="150"
                style="width: 915px;"
                type="image"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column v-if="columns.visible('title')" prop="title" label="故事名称"/>
        <el-table-column v-if="columns.visible('content')" prop="content" label="故事描述"/>
        <el-table-column v-if="columns.visible('productType')" prop="productType" label="产品分类">
          <template slot-scope="scope">
            {{ getVal(scope.row.productType) }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('pic')" prop="pic" label="图片地址">
          <template slot-scope="scope">
            <a :href="scope.row.pic" style="color: #42b983" target="_blank">
              <img :src="scope.row.pic" alt="点击打开" class="table-img"/>
            </a>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','story:edit','story:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
                :data="scope.row"
                :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import { getCates } from '@/api/yxStoreCategory'
import crudStory from '@/api/story'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from '@/components/material'

// crud交由presenter持有
const defaultCrud = CRUD({ title: 'Story', url: 'api/story', sort: 'id,desc', crudMethod: { ...crudStory } })
const defaultForm = { id: null, title: null, content: null, productType: null }
export default {
  name: 'Story',
  components: { pagination, crudOperation, rrOperation, udOperation, MaterialList },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['user_status'],
  data() {
    return {
      picArr: [],
      permission: {},
      rules: {},
      category: [],
      queryTypeOptions: [
        { key: 'title', display_name: '故事名称' }
      ]
    }
  },
  mounted() {
    getCates().then(async res => {
      this.category = res.content
    })
  },
  watch: {
    'picArr': function(val) {
      if (val) {
        this.form.pic = val.join(',')
      }
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      } else {
        delete this.crud.params.title
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.pic && form.id) {
        this.picArr = form.pic.split(',')
      } else {
        this.picArr = []
      }
    },
    getVal(id) {
      for (let categoryElement of this.category) {
        if (categoryElement.id === id) {
          return categoryElement.cateName
        }
      }
      return id
    }
  }
}


</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
